package com.info.zhiduoduo.wxsdk.config;

import com.github.wxpay.sdk.WXPayConfig;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2017/10/11 0011.
 */
@Component
@Slf4j
public class WxZddMakerPayConfig implements WXPayConfig{

    WxZddMakerConfig wxZddMakerConfig;

    private byte[] certData;

    public WxZddMakerPayConfig(WxZddMakerConfig wxZddMakerConfig){
        this.wxZddMakerConfig = wxZddMakerConfig;

        if(certData==null || certData.length==0) {
            try {
                String certPath =  wxZddMakerConfig.getCertfilepath()+"/apiclient_cert.p12";
                File file = new File(certPath);
                InputStream certStream = new FileInputStream(file);
                this.certData = new byte[(int) file.length()];
                certStream.read(this.certData);
                certStream.close();
            } catch (IOException e) {
                log.error("WxZddMakerPayConfig load Exception:" + e.getMessage());
            }
        }
    }

    @Override
    public String getAppID() {
        return wxZddMakerConfig.getAppid();
    }

    @Override
    public String getMchID() {
        return wxZddMakerConfig.getPayMchId();
    }

    @Override
    public String getKey() {
        return wxZddMakerConfig.getPayKey();
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}
